import java.util.Scanner;
public class StackByArray {
    private int[] stack;
    private int top;
    public StackByArray(int size) {
        stack = new int[size];
        top = -1;
    }
    public boolean isEmpty() {
        return top == -1;
    }
    public boolean isFull() {
        return top == stack.length - 1;
    }
    public boolean push(int data) {
        if (isFull()) {
            System.out.println("栈满");
            return false;
        } else {
            top++;
            stack[top] = data;
            return true;
        }
    }
    public int pop() {
        if (isEmpty()) {
            System.out.println("栈空");
            return -1; // 返回 -1 表示栈空
        } else {
            int data = stack[top]; // 获取栈顶元素
            top--; // 弹出栈顶元素
            return data; // 返回栈顶元素
        }
    }
    public static void convertToBase(int number, int base, StackByArray stack) {
        if (base < 2 || base > 16) {
            throw new IllegalArgumentException("进制必须是2，8，16中的一个");
        }
        if (number == 0) {
            stack.push('0');
            return;
        }
        String digits = "0123456789ABCDEF";
        while (number > 0) {
            int remainder = number % base;
            stack.push(digits.charAt(remainder));
            number /= base;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            // 提示用户输入一个非负的十进制整数
            System.out.println("请输入一个非负的十进制数字（或输入 exit 退出）：");
            String input = sc.nextLine();
            if (input.equals("exit")) {
                System.out.println("退出程序");
                break;
            }
            try {
                int data = Integer.parseInt(input);
                // 提示用户选择要转换的进制
                System.out.println("请输入想要转换为几进制的数字(2,8,16)：");
                int base = sc.nextInt();
                sc.nextLine(); // 清除缓冲区中的换行符
                // 根据用户选择的进制估算栈的最大容量
                int maxSize = 1; // 默认最大容量为 1
                if (data > 0) {
                    switch (base) {
                        case 2:
                            maxSize = (int) (Math.log(data) / Math.log(2)) + 1;
                            break;
                        case 8:
                            maxSize = (int) (Math.log(data) / Math.log(8)) + 1;
                            break;
                        case 16:
                            maxSize = (int) (Math.log(data) / Math.log(16)) + 1;
                            break;
                        default:
                            System.out.println("请输入正确的进制！");
                            continue;
                    }
                }
                // 创建一个 StackByArray 对象，栈的最大容量由估算得到
                StackByArray stack = new StackByArray(maxSize);
                // 进行转换
                convertToBase(data, base, stack);
                // 输出转换后的结果
                System.out.print("转换后的 " + base + " 进制数为: ");
                while (!stack.isEmpty()) {
                    int value = stack.pop();
                    System.out.print((char) value + " ");
                }
                System.out.println();
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }
        sc.close();
    }
}
